package com.my.blog.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.dao.UploadVoMapper;
import com.my.blog.website.modal.Vo.UploadVo;
import com.my.blog.website.service.IUploadService;
import com.my.blog.website.utils.DateKit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by wangq on 2017/3/20.
 */
@Service
public class UploadServiceImpl implements IUploadService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadServiceImpl.class);

    @Resource
    private UploadVoMapper uploadDao;

    /**
     * 获取当天上传的文件
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<UploadVo> getTodayUploads(Integer uid, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Example example = new Example(UploadVo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag",Boolean.FALSE);

        Date now = new Date();
        Date start = DateKit.getStartOfDate(now);
        Date end = DateKit.getEndOfDate(now);
        if(null != start && null != end){
            criteria.andBetween("created",start,end);
        }
        if (null!=uid){
            criteria.andEqualTo("authorId",uid);
        }

        example.setOrderByClause("id desc");
        List<UploadVo> uploadVos = uploadDao.selectByExample(example);
        return new PageInfo<>(uploadVos);
    }

    @Override
    public UploadVo selectById(Long id) {
        if(null != id){
            return uploadDao.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    @Transactional
    public void save(UploadVo uploadVo) {
        uploadVo.setDelFlag(Boolean.FALSE);
        uploadVo.setCreated(DateKit.getNowTime());

        uploadDao.insertSelective(uploadVo);

        System.out.print(uploadVo.getUrl());
        String url = WebConst.CONTEXT_PATH + "/static/upload/" + uploadVo.getId();
        uploadVo.setUrl(url);
        uploadVo.setMarkdownCode("!["+ uploadVo.getFname() +"]("+ url +")");
        uploadDao.updateByPrimaryKeySelective(uploadVo);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        if (null != id) {
            uploadDao.deleteByPrimaryKey( id);
        }
    }

    @Override
    @Transactional
    public void update(UploadVo uploadVo) {
        if (null != uploadVo) {
            uploadDao.updateByPrimaryKeySelective(uploadVo);
        }
    }

    /**
     * 根据条件获取列表  mybatis 的   and or 混合查询
     *
     * @param subPath
     * @param ftype
     * @param keyword
     * @param delFlag
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<UploadVo> getListByCondition(Integer uid, String subPath,String ftype,String keyword,String delFlag, Integer page, Integer limit) {
        if (null == page || page < 1){
            page = 1;
        }
        if (null == limit || limit < 1){
            limit = 10;
        }

        PageHelper.startPage(page, limit);
        Example example = new Example(UploadVo.class);

        Example.Criteria criteria = example.createCriteria();

        if (null!=uid){
            criteria.andEqualTo("authorId",uid);
        }
        if (StringUtils.isNotBlank(subPath) && !"null".equals(subPath)){
            criteria.andEqualTo("subPath",subPath);
        }
        if (StringUtils.isNotBlank(ftype) && !"null".equals(ftype)){
            criteria.andEqualTo("ftype",ftype);
        }
        if (StringUtils.isNotBlank(delFlag) && !"null".equals(delFlag)){
            //boolean delflag = "0".equals(delFlag) ? Boolean.FALSE : Boolean.TRUE;
            criteria.andEqualTo("delFlag",!"0".equals(delFlag));
        }
        if (StringUtils.isNotBlank(keyword) && !"null".equals(keyword)){
            keyword = "%"+ keyword +"%";
            criteria.andLike("fname",keyword);

            Example.Criteria criteria2 = example.createCriteria();
            if (StringUtils.isNotBlank(subPath) && !"null".equals(subPath)){
                criteria2.andEqualTo("subPath",subPath);
            }
            if (StringUtils.isNotBlank(ftype) && !"null".equals(ftype)){
                criteria2.andEqualTo("ftype",ftype);
            }
            if (StringUtils.isNotBlank(delFlag) && !"null".equals(delFlag)){
                //boolean delflag = "0".equals(delFlag) ? Boolean.FALSE : Boolean.TRUE;
                criteria.andEqualTo("delFlag",!"0".equals(delFlag));
            }
            criteria2.andLike("showName",keyword);

            example.or(criteria2);
        }

        example.setOrderByClause("id desc");
        List<UploadVo> uploadVos = uploadDao.selectByExample(example);
        return new PageInfo<>(uploadVos);
    }
}
